package com.project.erp.dao.system;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.project.erp.common.dao.HibernateDao;
import com.project.erp.common.dao.PropertyFilter;
import com.project.erp.common.utils.Page;
import com.project.erp.model.system.User;

@Repository
public class UserDao extends HibernateDao<User, Long>{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public String getStatusById(Long id){
		String status = (String) this.find("select u.status from User u where u.id = ?", id).get(0);
		return status;
	}
	
	public Page<User> getUserByPage(Page<User> page,List<PropertyFilter> propertyFilters){
		return this.findPage(page,propertyFilters);
	}
	
	public Page<User> getUserPageBySearch(Page<User> page,User user){
		if(null == user){
			page = this.getAll(page);
		}
		List<Criterion> list = new ArrayList<Criterion>();
		if(StringUtils.isNotEmpty(user.getRealName())){
			Criterion name = Restrictions.ilike("realName",user.getRealName(),MatchMode.ANYWHERE);
			list.add(name);
		}
		if(StringUtils.isNotEmpty(user.getLoginName())){
			Criterion loginName = Restrictions.ilike("loginName",user.getLoginName(),MatchMode.ANYWHERE);
			list.add(loginName);
		}
		if(StringUtils.isNotEmpty(user.getEmail())){
			Criterion email = Restrictions.ilike("email",user.getEmail(),MatchMode.ANYWHERE);
			list.add(email);
		}
		if(user.getCreateStartTime() != null){
			Criterion createStartTime = Restrictions.ge("createTime", user.getCreateStartTime());
			list.add(createStartTime);
		}
		
		if(user.getCreateEndTime() != null){
			Criterion createEndTime = Restrictions.le("createTime",user.getCreateEndTime());
			list.add(createEndTime);
		}
		
		page = this.findPage(page,list.toArray(new Criterion[list.size()]));
		
		return page;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> queryUserByCriteria(List<Long> roleIds){
		Criteria c = this.getSession().createCriteria(User.class);
		c.createAlias("roleList","roleList");
		//roleIds为传进来进行查询的角色ID
		c.add(Restrictions.in("roleList.id", roleIds));
		c.setFetchMode("roleList", FetchMode.JOIN);
		/*c.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);*/
		List<User> users = c.list();
		return users;
	}
	
	public int delUser(List<Long> userIds){
		String hql = "delete from User u where u.id in(:ids)";
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("ids", userIds);
		return batchExecute(hql,params);
	}
	
	//删除用户角色关联表数据
	public void deleteUserRoleByuserIds(final List<Long> userIds){
		String sql = "delete from tbl_user_role where user_id=?";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter(){
		@Override
		public void setValues(PreparedStatement ps, int i)
				throws SQLException {
			ps.setLong(1, userIds.get(i));
		}
		@Override
		public int getBatchSize() {
			return userIds.size();
		}});
	}

	@SuppressWarnings("unchecked")
	public User queryUserByLoginName(String loginName){
		Criteria c = this.getSession().createCriteria(User.class);
		c.add(Restrictions.eq("loginName", loginName));

		List<User> users = c.list();
		User user = null;
		
		if((users != null) && (users.size() > 0)){
			user = users.get(0);
		}
		return user;
	}
}
 